package com.qmgyl.marketshop.api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.qmfresh.service.common.dto.ListWithPage;
import com.qmfresh.service.entity.AdminUser;
import com.qmfresh.service.entity.OrderDelivery;
import com.qmfresh.service.entity.Sku;
import com.qmfresh.service.gateway.dto.AdminUserLogin;
import com.qmfresh.service.gateway.dto.DeliveryDetailQuery;
import com.qmfresh.service.product.facade.dto.ProductQuery;
import com.qmfresh.service.shop.facade.dto.LoginBean;
import com.qmfresh.service.shop.facade.dto.ShopSkuQuery;
import com.qmfresh.service.shop.facade.dto.SkuBind;
import com.qmfresh.service.shop.facade.vo.ShopSkuResult;
import com.qmgyl.marketshop.common.HttpUtils;
import com.qmgyl.marketshop.vo.DeliveryQuery;
import com.qmgyl.marketshop.vo.OrderDeliveryItemResult;
import com.qmgyl.marketshop.vo.OrderStatsResult;
import com.qmgyl.marketshop.vo.ServiceResult;

public class ServiceApi {
	
	private String serviceUrl;
	
	
	public String getServiceUrl() {
		return serviceUrl;
	}

	public void setServiceUrl(String serviceUrl) {
		this.serviceUrl = serviceUrl;
	}

	/**
	 * 实时订单统计数据
	 * @return
	 */
	public OrderStatsResult realTimeStats() {
		String result = HttpUtils.postRequestBody(serviceUrl + "/shop/stats/realtime",null);
		System.out.println(result);
		ServiceResult<OrderStatsResult> sr = JSONObject.parseObject(result, new TypeReference<ServiceResult<OrderStatsResult>>(){});
		return sr.getBody();
	}
	
	/**
	 * 按时间段统计订单数据
	 * @return
	 */
	public List<OrderStatsResult> daysOrderStats(Date start,Date end) {
		int s = (int)(start.getTime()/1000);
		int e = (int)(end.getTime()/1000);
		Map<String,Integer> params = new HashMap<String,Integer>();
		params.put("startTime", s);
		params.put("endTime", e);
		String result = HttpUtils.postRequestBody(serviceUrl + "/shop/stats/days",params);
		System.out.println(result);
		ServiceResult<List<OrderStatsResult>> sr = JSONObject.parseObject(result, new TypeReference<ServiceResult<List<OrderStatsResult>>>(){});
		return sr.getBody();
	}
	
	/**
	 * 查询订单配送列表
	 * @return
	 */
	public ListWithPage<OrderDelivery> orderDeliveryList(DeliveryQuery query) {
		String result = HttpUtils.postRequestBody(serviceUrl + "/shop/delivery/list",query);
		System.out.println(result);
		ServiceResult<ListWithPage<OrderDelivery>> sr = JSONObject.parseObject(result, new TypeReference<ServiceResult<ListWithPage<OrderDelivery>>>(){});
		return sr.getBody();
	}
	
	/**
	 * 查询订单配送列表
	 * @return
	 */
	public List<OrderDeliveryItemResult> listDeliveryItem(DeliveryDetailQuery query) {
		String result = HttpUtils.postRequestBody(serviceUrl + "/shop/delivery/detail",query);
		System.out.println(result);
		ServiceResult<List<OrderDeliveryItemResult>> sr = JSONObject.parseObject(result, new TypeReference<ServiceResult< List<OrderDeliveryItemResult>>>(){});
		
		return sr.getBody();
	}
	
	/**
	 * 查询订单配送列表(用于导出excel)
	 * @return
	 */
	public List<OrderDeliveryItemResult> listAllDeliveryItem(DeliveryQuery query) {
		String result = HttpUtils.postRequestBody(serviceUrl + "/shop/delivery/listDetail",query);
		System.out.println(result);
		ServiceResult<List<OrderDeliveryItemResult>> sr = JSONObject.parseObject(result, new TypeReference<ServiceResult< List<OrderDeliveryItemResult>>>(){});
		
		return sr.getBody();
	}
	
	/**
	 * 店仓已绑定SKU列表
	 * @param query
	 * @return
	 */
	public ListWithPage<Sku> skuBoundList(ShopSkuQuery query){
		String result = HttpUtils.postRequestBody(serviceUrl + "/shop/sku/skuBoundList",query);
		System.out.println(result);
		ServiceResult<ListWithPage<Sku>> sr = JSONObject.parseObject(result, new TypeReference<ServiceResult<ListWithPage<Sku>>>(){});
		return sr.getBody();
	}
	
	/**
	 * 店仓未绑定SKU列表
	 * @param query
	 * @return
	 */
	public ListWithPage<Sku> skuUnbindList(ProductQuery query){
		String result = HttpUtils.postRequestBody(serviceUrl + "/shop/sku/skuUnbindList",query);
		System.out.println(result);
		ServiceResult<ListWithPage<Sku>> sr = JSONObject.parseObject(result, new TypeReference<ServiceResult<ListWithPage<Sku>>>(){});
		return sr.getBody();
	}
	
	/**
	 * 店仓操作绑定SKU
	 * @param query
	 * @return
	 */
	public boolean bindSku(SkuBind skuBind){
		String result = HttpUtils.postRequestBody(serviceUrl + "/shop/sku/bindSku",skuBind);
		System.out.println(result);
		ServiceResult sr = JSONObject.parseObject(result,ServiceResult.class);
		return sr.getSuccess();
	}
	
	/**
	 * 登录接口
	 * @param query
	 * @return
	 */
	public ServiceResult<AdminUser> login(LoginBean bean){
		String result = HttpUtils.postRequestBody(serviceUrl + "/shop/login",bean);
		System.out.println(result);
		ServiceResult<AdminUser> sr = JSONObject.parseObject(result, new TypeReference<ServiceResult<AdminUser>>(){});
		return sr;
	}
	
	/**
	 * token校验
	 * @param query
	 * @return
	 */
	public AdminUserLogin ssoValidate(LoginBean bean){
		String result = HttpUtils.postRequestBody(serviceUrl + "/shop/validate",bean);
		System.out.println(result);
		ServiceResult<AdminUserLogin> sr = JSONObject.parseObject(result, new TypeReference<ServiceResult<AdminUserLogin>>(){});
		return sr.getBody();
	}
}
